package com.jinyang.jetpackdemo.activity.ui.duty

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.jinyang.jetpackdemo.databinding.FragmentDutyBinding
import com.jinyang.jetpackdemo.util.LjyLogUtil

class DutyListFragment : Fragment() {

    private lateinit var dutyListViewModel: DutyListViewModel
    private var _binding: FragmentDutyBinding? = null

    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!

    override fun onAttach(context: Context) {
        super.onAttach(context)
        LjyLogUtil.d("Fragment.onAttach")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LjyLogUtil.d("Fragment.onCreate:${savedInstanceState}")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dutyListViewModel =
            ViewModelProvider(this).get(DutyListViewModel::class.java)

        _binding = FragmentDutyBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val textView: TextView = binding.textHome
        dutyListViewModel.text.observe(viewLifecycleOwner, Observer {
            textView.text = it
        })
        return root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        LjyLogUtil.d("Fragment.onActivityCreated:${savedInstanceState}")
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        LjyLogUtil.d("Fragment.onViewCreated:${view}__${savedInstanceState}")
    }

    override fun onStart() {
        super.onStart()
        LjyLogUtil.d("Fragment.onStart")
    }

    override fun onResume() {
        super.onResume()
        LjyLogUtil.d("Fragment.onResume")
    }

    override fun onPause() {
        super.onPause()
        LjyLogUtil.d("Fragment.onPause")
    }

    override fun onStop() {
        super.onStop()
        LjyLogUtil.d("Fragment.onStop")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
        LjyLogUtil.d("Fragment.onDestroyView")
    }

    override fun onDestroy() {
        super.onDestroy()
        LjyLogUtil.d("Fragment.onDestroy")
    }

    override fun onDetach() {
        super.onDetach()
        LjyLogUtil.d("Fragment.onDetach")
    }


    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        LjyLogUtil.d("Fragment.onSaveInstanceState:${outState}")
        outState.putString("key3","value3")
        outState.putString("key4","value4")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        LjyLogUtil.d("Fragment.onConfigurationChanged:${newConfig}")
    }
}